Central Article

The Building Blocks of a Central Application

Vera Fleischer

 

I have built a little Central application for the sole purpose of demonstrating how the building blocks of a Central application work together. All the source files are available for download. The application doesn't really do a lot: It opens a pod, connects to an agent through LCService, sends notices when a user clicks a button, and blasts a zip code when you tell it to. I'm assuming that you know Flash and ActionScript, which is why the application doesn't really have any meat. I am sure most of you have your own ideas for useful Central applications. This one is just for you to pick apart and learn from.

Requirements

To complete this tutorial you will need to install the following software and files:

Macromedia Flash MX Professional 2004

Macromedia Central

Install Central now (free) ›

Macromedia Central SDK

Download it now ›

Tutorials and sample files:

The Application

When you build a Central application, you generally store all of its files in a folder with the same name as the application. That folder contains the following:

  • A FLA file with the same name as the folder
  • A pod folder
  • An agent folder (if applicable)
  • An icons folder
  • Any other files needed for the application

For example, if the name of your application is SampleApp, you would create a folder on your hard drive called SampleApp and save a FLA file in it called SampleApp.fla (see Figure 1).

Folder structure for SampleApp Central application

Figure 1. Folder structure for SampleApp Central application

For this application, the pod and agent folders each contain an init.as file, and the pod.fla and agent.fla files each contain one line of code to include their respective init.as files. Here's an example of the pod include:

#include "pod/init.as"

I have also created interface.as, which contains all the LCService logic for the agent—I'll give you more information on this file later. You must #include it in both the application and the agent. Below is the basic skeleton of the application code contained in SampleApp.fla.

#include "Interface.as"

// called by Central when app is loaded
onActivate = function(shell, appId, shellId, baseTabIndex, appData) {
	vShell = shell;
	// agent reference
	agentProps = vShell.getAgent();
	// pod reference
	pods = vShell.getPods();
	myPod = pods[0];
}

// called by Central when app is unloaded
onDeactivate = function(shell, appId, shellId, baseTabIndex, appDate) {
	
	// abort all open processes
	
}

// setting min and max size
getMinimumSize = function() {
	var sObj = new Object();
	sObj.width = 500;
	sObj.height = 500;
	return sObj;
}

getMaximumSize = function() {
	var sObj = new Object();
	sObj.width = 500;
	sObj.height = 500;
	return sObj;
}
	
// init the Console
Central.initApplication(this, this);

The code has onActivate and onDeactivate functions, which Central calls automatically. It also has getMinimumSize and getMaximumSize functions, which ensure that the application window stays within specified size limits. In this case, I decided to keep the size the same at all times.

The Pod

When building the application, I wanted to make sure that only one pod is open when I make a call to viewPod() during the onActivate()function. But how do you determine if the pod is still open from the last time you viewed the application? If you don't detect pods that are already open, you will end up with multiple, identical pods—one for each time you restart the application without closing Central.

You can solve the problem with the almost "magical" method, getViewedPods(). Note that you put this code in the application FLA, not the pod:

// get array of pods
pods = shell.getPods();
// there is only one pod for this app, so it must be the first in the list
myPod = pods[0];
// get viewed pods
viewedPods = shell.getViewedPods();
if (viewedPods.length == 0) {
   // if the array of viewed pods has a length of 0, no pod is currently
   // open for this app, so it's safe to open one; otherwise don't
   shell.viewPod(myPod.id);
}

Every time the application activates, I check if there are any viewed pods. If (and only if) there aren't any, I open the pod.

Sample pod

Figure 2. Sample pod

Below is the basic structure of the init.as file the pod uses. Again, this is just a skeleton. The pod in the sample application also includes a function to receive notices, which I will explain later.

function onActivate(dock, podid, viewerId, position, baseTabIndex, data)
{
	// create console reference
this.gConsole = dock;
	
}

function onDeactivate()
{
	/* 
	put other deactivation code here
	Example: code to store data locally
	*/
}

function onNetworkChange( connected )
{
	/* 
	put code here that will execute when the user
	is disconnected from internet
	*/
}

// any other functions you may need
function anotherPodFunction() {
	…	
}

// init the pod

Central.initPod( this, this );

The Agent

The most common use for an agent is as the server in an LCService. An LCService, or Local Connection Service, always establishes a connection between a server and a client. Do not confuse this with an HTTP server, however; in this case, "server" merely means that the agent serves or processes data, which the client consumes. In this case, the client is the application.

When you use LCService, there are a few lines of code that the client and server share. It makes sense to create a separate file, which you then #include in both. In my example, Interface.as is the shared file; it defines the way the client and server will interface:

IAgent = new Object();
IAgent.name = "IAgent";
IAgent.interfaces = new Object();
IAgent.interfaces.Client = [];
IAgent.interfaces.Server = ["doRespond"];

The interface specifies two sets of functions, which the server can call on the client and vice versa. In this specific example, the client doesn't make any methods available to the server, but the server defines a doRespond method. The client can invoke this method.

Note that the interface doesn't actually set up the LCService between the client and server; it merely provides an interface. The syntax for setting up the client and server is as follows.

// Creating the client, usually the app itself
serviceName = Central.LCService.createClient(agentName, clientName, this, false);

// Creating the server, usually the agent
serviceName = Central.LCService.createServer(agentName, this, false);

The line containing createClient() goes in the application code, or in whichever other actor you want to be the client. The line containing createServer() goes in the server code, in this case, the init.as file in the agent folder.

The variables serviceName and agentName must be the same on the client side and the server side. Note that if you want this to be a synchronous service, the last parameter should be true on both sides.

You can start and stop the agent can from the application with Shell.startAgent() and Shell.stopAgent().

For more information on LCService, please refer to Ethan Malasky's article, Understanding and Using the LCService Library.

Notices

The application and the pod each have a sayHi() method which sends a notice—in this case a "hi" message. Below is the sayHi() function the pod uses:

// Function that sends a notice from the pod
function sayHi() {
	// Creates a new notice object
	notice = new Object();
	notice.name = "The pod said hi!";
	notice.description = "This is an example of a Central notice.";
	notice.alert = true;
	notice.timeout = 30;
	notice.navigate = true;
	notice.engageString = "React";
	this.gConsole.addNotice(notice);
}

Here's what some of the properties of the notice in the sayHi()function do. The timeout property makes the notices disappear after 30 seconds so that they don't clutter your screen. To have another part of the application react to a notice, you have to set up an engage link. The engageString property specifies which text to display for the engage link inside the notice. In this case, a link titled "React" will send information back to the application. To handle the user engagement, you have to write an onNoticeEvent function in either the application, the agent, or the pod, or in all of them, depending on which part of your application you want to handle the notice event. I included it in the application itself, and it looks like this:

onNoticeEvent = function(event, noticeData) {
   if (event.type == "engage") {
      star.gotoAndPlay("dance");
   }
}

Very simple. It plays a little animation in the main application window, indicating that the user clicked the engage link.

Sending a Blast

A blast lets you share data among different Central applications. The data has to be of a specified standard format so that it can be understood across the board. The sample application sends a zip code blast when a user clicks the "blast it" button. The onRelease handler on the button contains the following code:

// create address object
myAddress = {};
// get zip code from input text field
myAddress.postalCode = zipcode_txt.text;
myAddress.country = "USA";
// create SelectedItem object
var addressSI = new SelectedItem("http://download.macromedia.com/pub/central/schemas/CentralData.xsd", "address");
addressSI.address = myAddress;
// setSelectedItem
dataArray = new Array(addressSI);
vShell.setSelectedItem(dataArray, "You blasted a zip code!");

First, you create an object (myAddress) to hold the data you want to blast. Then you create a SelectedItem object of type address, as defined by the address schema on the Macromedia website (at http://download.macromedia.com/pub/central/schemas/CentralData.xsd) After calling setSelectedItem(), a clickable blast icon will appear in the lower right corner of the console when you click the "blast it" button.

 Sample blast

Figure 3. Sample blast

Receiving a Blast

To receive the Blast sent from the sample application, I made another Central application that I set up to receive address blasts. I've included source files for this in the sample files download as well. Again, it's very simple and certainly no killer application; I'm just trying to show how to use a blast sent from another application. As you can probably guess, I've set this application up to receive a zip code from the sample application and then do something with it. The blast receiver application connects to a web service to look up information about any U.S. zip code. It could also look up information about area codes, but I didn't set up a blast for that.

To receive the zip code blast from the sample application, I put the following code inside the Zip and Area Code Info application:

// function that is called when a Blast is received
onSelectedItem = function (data) {
   // loop through the data
   for (var i = 0; i<data.length; i++) {
      // this app only accepts addresses, so check for that
      if (data[i].address != null) {
         // display the received zip code in text field
         var zip = data[i].address.postalCode;
         container.selectedChild.inputText.text = zip;
         container.selectedChild.blasted._visible = true;
      }
   }
}

For a blast to work correctly, the following also necessary in the product.xml file of the application receiving a blast. It is not necessary in the one sending a blast.

<supportedTypes namespace="http://www.w3.org/2001/XMLSchema" schema="http://download.macromedia.com/pub/central/schemas/CentralData.xsd">
<type>any</type>
</supportedTypes>

If the "blastable" data originates in the pod, you insert it in the <pod> tags. If it originates in the application itself, you insert it in the <application> tags. For my example, I put it within the <application> tags, and my blast feature was complete. Instead of type any, you can also be more restrictive. The list of available types includes email, im, link, coordinates, phone, contact, publication, business, location, and, of course, address.

The product.xml File

The product.xml file for the sample application looks like this:

<product name="Vera's Sample App"
    created="02/01/2004"
    version="1.1"
    price="Free"
    requireVersion="1"
    help="productHelp.html"
    category="Personal Productivity"
    productID="XXXXXX-XXXXXX-XXXXXX-XXXXXX">
    <author name="Vera Fleischer" info="http://www.mediasparkles.com/"/>
    <description>The Sample App demonstrates use of a Pod, an Agent, Notifications, and Blast.
</description>
    <icon src="icons/sample_40.swf" size="40"/>
    <application name="VSample App"
    src="SampleApp.swf"
    width="500" height="500"
    help="appHelp.html"
    lang="en"
    background="#FFFFFF"
    version="1.1"
    sendto="true">
<icon src="icons/sample_40.swf" size="40"/>
    <pod name="Sample App Pod"
    src="pod.swf"
    height="120"/>
    <agent name="Sample App Agent"
    src="agent.swf"/>
    </application>
    </product>

The <pod> and <agent> tags are only necessary if your application includes a pod and an agent. As a comparison, take the product.xml for the Zip Code Info application:

<product name="Zip & Area Code Info"
    created="02/01/2004"
    version="1.0"
    price="Free"
    requireVersion="1"
    help="productHelp.html"
    category="Personal Productivity"
    productID=" XXXXXX-XXXXXX-XXXXXX-XXXXXX">
    <author name="Vera Fleischer" info="http://www.mediasparkles.com/"/>
    <description>This app looks up data based on a U.S. zip code or area code via a web service.
</description>
    <icon src="icons/sample_40.swf" size="40"/>
    <application name="Zip & Area Code Info"
    src="zipcodeinfo.swf"
    width="500" height="500"
    help="appHelp.html"
    lang="en"
    background="#FFFFFF"
    version="1.0"
    sendto="true">
    <supportedTypes namespace="http://www.w3.org/2001/XMLSchema" schema="http://download.macromedia.com/pub/central/schemas/CentralData.xsd">
      <type>any</type> 
      </supportedTypes>
<icon src="icons/sample_40.swf" size="40"/>
    </application>
    </product>

This product.xml file does not have a pod or an agent section because the application doesn't include any. It does, however, use the <supportedTypes> tag in order to make it "blast-ready." Another thing to note about the product.xml file is that as you release new versions of your applications, all you have to do is increment the value of the version tag and reupload the product.xml file to the server on which your application's installer is located. The next time a user starts Central, it will automatically compare the version in the product.xml file to the version the user currently has installed and prompt her to update, if her preferences are set to Automatically check for updates.


About the author

Vera Fleischer has been a slave to Flash since 2000. She first started playing with it after she accidentally opened it at a small e-learning shop in Charlottesville, Virginia. After about two years of developing educational content with Flash, she moved to San Francisco so that she could be closer to Macromedia, the home of Flash. In San Francisco, she loitered near the Macromedia building until somebody finally hired her. Vera is now a QA Engineer on the Flash team. When she is not at work, you can find Vera at www.mediasparkles.com, and she welcomes any questions you may have about this article.